<template>
  <div class="app-container">
    <!-- 查询和其他操作 -->
    <div class="filter-container">
      <el-input v-model="listQuery.name" clearable class="filter-item" style="width: 200px;" placeholder="请输入标题"/>
      <el-input v-model="listQuery.content" clearable class="filter-item" style="width: 200px;" placeholder="请输入内容"/>
      <el-button v-permission="['GET /admin/college/list']" class="filter-item" type="primary" icon="el-icon-search"
                 @click="getList(true)">查找
      </el-button>
      <el-button v-permission="['POST /admin/college/create']" class="filter-item" type="primary" icon="el-icon-edit"
                 @click="handleCreate">添加
      </el-button>
    </div>
    <!-- 查询结果 -->
    <el-table v-loading="list == undefined" :data="list" element-loading-text="正在查询中。。。" border fit highlight-current-row>
      <el-table-column width="60" align="center" label="ID" prop="id"/>
      <el-table-column prop="title" label="标题" />
      <el-table-column prop="duration" label="时长" />
<!--      <el-table-column prop="coverLink" width="80" label="封面" show-overflow-tooltip >-->
<!--        <template slot-scope="scope">-->
<!--          <el-image style="width: 50px;height: 30px;" :preview-src-list="[scope.row.coverLink]" :src="scope.row.coverLink" :fit="'cover'"></el-image>-->
<!--        </template>-->
<!--      </el-table-column>-->
      <el-table-column prop="addTime" label="创建时间" show-overflow-tooltip />
      <el-table-column prop="videoType " label="视频类型">
        <template slot-scope="scope">
          <el-tag type="success">{{ scope.row.videoType  == 0 ? '学院' : '小视频' }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column align="center" width="70" prop="status" label="状态">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.status == 0"
            active-color="#13ce66"
            inactive-color="#ff4949"
            @change="isStatus($event,scope.row)">
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column prop="content" label="描述" show-overflow-tooltip />
      <el-table-column align="center" label="操作" width="200" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button v-permission="['POST /admin/college/update']" type="primary" size="mini"
                     @click="handleUpdate(scope.row)">编辑
          </el-button>
          <el-button v-permission="['POST /admin/college/delete']" type="danger" size="mini"
                     @click="handleDelete(scope.row)">删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit"
                @pagination="getList"/>
    <!-- 添加或修改对话框 -->
    <el-dialog top="5vh" :title="dataForm && (dataForm.id ? '编辑' : '创建')" :visible.sync="isDiglog" append-to-body>
      <add-form v-if="dataForm" :dataForm="dataForm" :cancel="cancel"></add-form>
    </el-dialog>
  </div>
</template>

<script>
  import { list, remove, isStatus } from '@/api/apiCourse'
  import AddForm from './add'
  import Pagination from '@/components/Pagination' // Secondary package based on el-pagination

  export default {
    name: 'course',
    components: { Pagination, AddForm },
    props: ['checkCollegeId'],
    data() {
      return {
        dataForm: undefined,
        list: undefined,
        total: 0,
        isDiglog: false,
        listQuery: {
          page: 1,
          limit: 20,
          title: undefined,
          content: undefined,
          sort: 'add_time',
          order: 'desc'
        }
      }
    },
    created() {
      this.getList()
    },
    methods: {
      isStatus(val,row) {
        let self = this
        isStatus({ids: row.id,status: val ? 0 : 1}).then(res => {
          row.status = val ? 0 : 1
        })
      },
      cancel() {
        this.isDiglog = false
        this.getList(true)
      },
      getList(init = false) {
        if (init === true) {
          this.listQuery.page = 1
        }
        list({...this.listQuery,...{ pid: this.checkCollegeId}})
          .then(response => {
            this.list = response.data.data.list
            this.total = response.data.data.total
          })
      },
      handleCreate() {
        this.dataForm = {link:[],collegeId: this.checkCollegeId}
        this.isDiglog = true
      },
      handleUpdate(rowData) {
        let row = {...rowData}
        row.link = [{url: row.link}]
        // row.coverLink = [{url: row.coverLink}]
        row.courseNumber = parseInt(row.courseNumber)
        this.dataForm = row
        this.isDiglog = true
      },
      handleDelete(row) {
        remove({ ids: row.id }).then(response => {
          this.$notify.success({
            title: '成功',
            message: '删除成功'
          })
          const index = this.list.indexOf(row)
          this.list.splice(index, 1)
        })
      }
    }
  }
</script>

<style scoped>

</style>
